﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using Museum.DBAccess;

public partial class VoucherDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int voucherID = 0;
        DataTable dtVoucherDetails = new DataTable();
        DataRow[] dtRows = null;
        DataTable dtGames = null;
        DataTable dtNewGames = null;
        int museumId = 0;
        if (!Page.IsPostBack)
        {
            string strWatermarkSearch = "0.00";
            txtFixedPrice.Text = strWatermarkSearch;
            txtFixedPrice.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermarkSearch + "');");
            txtFixedPrice.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermarkSearch + "');");

            txtPricePerVoucher.Text = strWatermarkSearch;
            txtPricePerVoucher.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermarkSearch + "');");
            txtPricePerVoucher.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermarkSearch + "');");

            dtGames = DBAccess.GetAllGames();

            if (Session["MuseumId"] != null)
            {
                museumId = Convert.ToInt32(Session["MuseumId"]);
            }

            dtRows = dtGames.Select("MuseumId=" + museumId);

            dtNewGames = dtGames.Clone();
            foreach (DataRow row in dtRows)
            {
                dtNewGames.ImportRow(row);
            }
            if (dtNewGames != null && dtNewGames.Rows.Count > 0)
            {
                ddlGame.DataSource = dtNewGames;
                ddlGame.DataBind();
            }

            if (Request.QueryString["VoucherID"] != null)
            {
                voucherID = Convert.ToInt32(Request.QueryString["VoucherID"]);
                //get and bind voucher details
                dtVoucherDetails = DBAccess.GetVoucherDetails(voucherID);
                if (dtVoucherDetails != null && dtVoucherDetails.Rows.Count > 0)
                {
                    txtValidTo.SelectedDate = Convert.ToDateTime(dtVoucherDetails.Rows[0]["VoucherExpireDate"]);
                    lblVoucherNumber.Text = dtVoucherDetails.Rows[0]["VoucherDetails"].ToString();
                    txtFixedPrice.Text = dtVoucherDetails.Rows[0]["VoucherFixedPrice"].ToString();
                    txtPricePerVoucher.Text = dtVoucherDetails.Rows[0]["PerVoucherPrice"].ToString();
                    txtClientName.Text = dtVoucherDetails.Rows[0]["ClientName"].ToString();
                    ddlGame.SelectedValue = dtVoucherDetails.Rows[0]["GameId"].ToString();
                }
            }

            txtValidFrom.Text = DateTime.Now.Date.ToString("yyyy-MMM-dd");
            txtValidFrom.Enabled = false;
            GenerateVoucher();
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        txtValidTo.Visible = true;
    }

    private void GenerateVoucher()
    {
        string voucherNumber = string.Empty;
        Random randomNumber = new Random();

        voucherNumber = randomNumber.Next(10000, 100000).ToString();
        if (ddlGame.Items.Count > 0)
            voucherNumber = ddlGame.SelectedItem.Text.Replace(" ", string.Empty).Substring(0, 3) + voucherNumber;
        lblVoucherNumber.Text = voucherNumber.ToUpper();
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        int result = 0;
        decimal fixedPrice = 0;
        decimal pricePerVoucher = 0;
        //StringBuilder vouchers = new StringBuilder();
        //string[] voucherDetails = null;
        try
        {
            //voucherDetails = lblVoucherNumber.Text.Split(',');
            //for (int i = 0; i < voucherDetails.Length; i++)
            //{
            if (!string.IsNullOrEmpty(txtFixedPrice.Text.Trim()))
                fixedPrice = Convert.ToDecimal(txtFixedPrice.Text.Trim());
            if (!string.IsNullOrEmpty(txtPricePerVoucher.Text.Trim()))
                pricePerVoucher = Convert.ToDecimal(txtPricePerVoucher.Text.Trim());

            result = DBAccess.InsertVoucher(Convert.ToInt32(ddlGame.SelectedValue), lblVoucherNumber.Text, txtValidTo.SelectedDate,
                txtClientName.Text.Trim(), fixedPrice, pricePerVoucher);
            //}

            if (result == -1)
                Alert.ShowMessage("Voucher created successfully.");
            else
                Alert.ShowMessage("Error occurred, please try again.");

            Response.Redirect("ManageVouchers.aspx", false);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ddlGame.SelectedIndex = 0;
        txtValidTo.SelectedDate = DateTime.Now.Date;
        lblVoucherNumber.Text = "";
    }
    protected void ddlGame_SelectedIndexChanged1(object sender, EventArgs e)
    {
        GenerateVoucher();
    }
    protected void txtValidTo_SelectionChanged(object sender, EventArgs e)
    {
        TextBox1.Text = txtValidTo.SelectedDate.ToString("yyyy-MMM-dd");

        txtValidTo.Visible = false;
    }
}